//
//  TGTopMenuItem.m
//  GroupBuy
//
//  Created by wwj on 15/11/4.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGTopMenuItem.h"
#import "UIImage+Extend.h"

#define kTitleScale 0.9

@implementation TGTopMenuItem

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

-(void)initView{
    // 1.文字颜色
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    
    // 2.设置箭头
    [self setImage:[UIImage imageNamed:@"ic_arrow_down"] forState:UIControlStateNormal];
    self.imageView.contentMode = UIViewContentModeCenter;
    
    // 3.右边的分割线
    UIImage *img = [UIImage imageNamed:@"separator_topbar_item"];
    UIImageView *divider = [[UIImageView alloc] initWithImage:img];
    divider.bounds = CGRectMake(0, 0, 2, kTopMenuItemH * 0.8);
    divider.center = CGPointMake(kTopMenuItemW, kTopMenuItemH * 0.5);
    [self addSubview:divider];
    
    // 4.选中时的背景
    [self setTitleColor:MyColor(52, 164, 215) forState:UIControlStateSelected];
    
}

-(void)setTitle:(NSString *)title{
    _title = title;
    [self setTitle:title forState:UIControlStateNormal];
}

-(void)setHighlighted:(BOOL)highlighted{}

-(void)setFrame:(CGRect)frame{
    frame.size = CGSizeMake(kTopMenuItemW, kTopMenuItemH);
    [super setFrame:frame];
}

-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat width = contentRect.size.width * kTitleScale;
    CGFloat height = contentRect.size.height;
    return CGRectMake(5, 0, width, height);
}

-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat X = contentRect.size.width * kTitleScale - 10;
    CGFloat W = contentRect.size.width * (1-kTitleScale);
    CGFloat H = contentRect.size.height;
    return CGRectMake(X, 0, W, H);
}
@end
